package ui;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Opciones extends javax.swing.JFrame {
	private Intro principal;
	private JPanel background;
	private JLabel blackboard;
	private ButtonGroup Dificultad, ModoJuego;
	private JRadioButton jRadioFacil, jRadioNormal, jRadioDificil;
	private JRadioButton jRadiojvsj, jRadiojvscp, jRadiocpvscp; 
	private JLabel RadioDificultadLabel, RadioModoJuegoLabel;
	private JButton BotonMenu;
	private JLabel Title;
	private JLabel info;
	
	/**
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Opciones inst = new Opciones();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}*/
	
	public Opciones(Intro principal) {
		super();
		this.principal=principal;
		
		initGUI();
	}
	
	private void initGUI() {
		try {			
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setMaximumSize(new java.awt.Dimension(640, 590));
			this.setMinimumSize(new java.awt.Dimension(640, 590));
			this.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent evt) {
					thisWindowClosing(evt);
				}
			});
			{
				background = new JPanel();
				getContentPane().add(background, BorderLayout.CENTER);
				background.setLayout(null);
				background.setSize(640, 590);
				
				this.Dificultad=new ButtonGroup();
				this.ModoJuego=new ButtonGroup();
				{
					info = new JLabel();
					background.add(info);
					info.setText("Jorge Merino Garcia");
					info.setBounds(480, 505, 120, 20);
					info.setBackground(new java.awt.Color(67,85,72));
					info.setFont(new java.awt.Font("Eraser",1,10));
					info.setForeground(new java.awt.Color(255,255,255));
					info.setOpaque(false);
					info.setBorder(BorderFactory.createEmptyBorder());
				}
				{
					BotonMenu = new JButton();
					background.add(BotonMenu);
					BotonMenu.setText("MENU");
					BotonMenu.setFont(new java.awt.Font("Eraser",1,20));
					BotonMenu.setForeground(new java.awt.Color(255,255,255));
					BotonMenu.setBackground(new java.awt.Color(67,85,72));
					BotonMenu.setOpaque(false);
					BotonMenu.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
					BotonMenu.setBounds(29, 497, 145, 40);
					BotonMenu.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							BotonMenuMouseClicked(evt);
						}
					});
				}				
				{
					jRadioFacil = new JRadioButton();
					background.add(jRadioFacil);
					jRadioFacil.setText("Facil");
					jRadioFacil.setFont(new java.awt.Font("Eraser",1,15));
					jRadioFacil.setForeground(new java.awt.Color(255,255,255));
					jRadioFacil.setBackground(new java.awt.Color(67,85,72));
					jRadioFacil.setOpaque(false);
					jRadioFacil.setBounds(150, 330, 100, 20);
					Dificultad.add(jRadioFacil);
					jRadioFacil.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jRadioFacilMouseClicked(evt);
						}
					});

				}
				{
					jRadioNormal = new JRadioButton();
					background.add(jRadioNormal);
					jRadioNormal.setText("Normal");
					jRadioNormal.setFont(new java.awt.Font("Eraser",1,15));
					jRadioNormal.setForeground(new java.awt.Color(255,255,255));
					jRadioNormal.setBackground(new java.awt.Color(67,85,72));
					jRadioNormal.setOpaque(false);
					jRadioNormal.setBounds(150, 350, 100, 20);
					jRadioNormal.setSelected(true);
					Dificultad.add(jRadioNormal);
					principal.setDificultad(getDificultadRadioButtons());
					System.out.println("Dificultad por defecto --> " + principal.getDificultad());
					
					jRadioNormal.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jRadioNormalMouseClicked(evt);
						}
					});

				}
				{
					jRadioDificil = new JRadioButton();
					background.add(jRadioDificil);
					jRadioDificil.setText("Dificil");
					jRadioDificil.setFont(new java.awt.Font("Eraser",1,15));
					jRadioDificil.setForeground(new java.awt.Color(255,255,255));
					jRadioDificil.setBackground(new java.awt.Color(67,85,72));
					jRadioDificil.setOpaque(false);
					jRadioDificil.setBounds(150, 370, 100, 20);
					Dificultad.add(jRadioDificil);
					jRadioDificil.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jRadioDificilMouseClicked(evt);
						}
					});
				}	
				{
					RadioDificultadLabel = new JLabel();
					background.add(RadioDificultadLabel);
					RadioDificultadLabel.setText("DIFICULTAD");
					RadioDificultadLabel.setBounds(130, 300, 150, 20);
					RadioDificultadLabel.setBackground(new java.awt.Color(67,85,72));
					RadioDificultadLabel.setFont(new java.awt.Font("Eraser",1,20));
					RadioDificultadLabel.setForeground(new java.awt.Color(255,255,255));
					RadioDificultadLabel.setOpaque(false);
					RadioDificultadLabel.setBorder(BorderFactory.createEmptyBorder());
				}
				{
					jRadiojvsj = new JRadioButton();
					background.add(jRadiojvsj);
					jRadiojvsj.setText("Jugador vs Jugador");
					jRadiojvsj.setFont(new java.awt.Font("Eraser",1,15));
					jRadiojvsj.setForeground(new java.awt.Color(255,255,255));
					jRadiojvsj.setBackground(new java.awt.Color(67,85,72));
					jRadiojvsj.setOpaque(false);
					jRadiojvsj.setBounds(150, 230, 200, 20);
					ModoJuego.add(jRadiojvsj);
					jRadiojvsj.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jRadiojvsjMouseClicked(evt);
						}
					});

				}
				{
					jRadiojvscp = new JRadioButton();
					background.add(jRadiojvscp);
					jRadiojvscp.setText("Jugador vs CPU");
					jRadiojvscp.setFont(new java.awt.Font("Eraser",1,15));
					jRadiojvscp.setForeground(new java.awt.Color(255,255,255));
					jRadiojvscp.setBackground(new java.awt.Color(67,85,72));
					jRadiojvscp.setOpaque(false);
					jRadiojvscp.setBounds(150, 250, 200, 20);
					jRadiojvscp.setSelected(true);
					ModoJuego.add(jRadiojvscp);
					principal.setModo(getModoJuegoRadioButtons());
					System.out.println("ModoJuego por defecto --> " + principal.getModo());
					
					jRadiojvscp.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jRadiojvscpMouseClicked(evt);
						}
					});

				}
				{
					jRadiocpvscp = new JRadioButton();
					background.add(jRadiocpvscp);
					jRadiocpvscp.setText("CPU vs CPU");
					jRadiocpvscp.setFont(new java.awt.Font("Eraser",1,15));
					jRadiocpvscp.setForeground(new java.awt.Color(255,255,255));
					jRadiocpvscp.setBackground(new java.awt.Color(67,85,72));
					jRadiocpvscp.setOpaque(false);
					jRadiocpvscp.setBounds(150, 270, 200, 20);
					ModoJuego.add(jRadiocpvscp);
					jRadiocpvscp.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jRadiocpvscpMouseClicked(evt);
						}
					});
				}	
				{
					RadioModoJuegoLabel = new JLabel();
					background.add(RadioModoJuegoLabel);
					RadioModoJuegoLabel.setText("Modo de Juego");
					RadioModoJuegoLabel.setBounds(130, 200, 200, 20);
					RadioModoJuegoLabel.setBackground(new java.awt.Color(67,85,72));
					RadioModoJuegoLabel.setFont(new java.awt.Font("Eraser",1,20));
					RadioModoJuegoLabel.setForeground(new java.awt.Color(255,255,255));
					RadioModoJuegoLabel.setOpaque(false);
					RadioModoJuegoLabel.setBorder(BorderFactory.createEmptyBorder());
				}	
				{
					Title = new JLabel();
					background.add(Title);
					Title.setText("TRES EN RAYA");
					Title.setBounds(80, 60, 510, 110);
					Title.setBackground(new java.awt.Color(67,85,72));
					Title.setFont(new java.awt.Font("Eraser",1,68));
					Title.setForeground(new java.awt.Color(255,255,255));
					Title.setOpaque(false);
					Title.setBorder(BorderFactory.createEmptyBorder());
				}				
				{
					blackboard = new JLabel();
					background.add(blackboard);
					blackboard.setBounds(0, -6, 640, 570);
					blackboard.setIcon(new ImageIcon(getClass().getClassLoader().getResource("ui/pizarra.png")));
				}
			}
			this.setSize(640, 590);
			pack();
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	private void BotonMenuMouseClicked(MouseEvent evt) {
		System.out.println("BotonMenu.mouseClicked, event="+evt);
		//TODO add your code for BotonMenu.mouseClicked
		principal.setVisible(true);
		this.dispose();
	}	
	
	private ButtonGroup getDificultad() {
		if(Dificultad == null) {
			Dificultad = new ButtonGroup();
			System.out.println("Error devolviendo el ButtonGroup de dificultad");
		}
		return Dificultad;
	}
	private ButtonGroup getModoJuego() {
		if(ModoJuego == null) {
			ModoJuego = new ButtonGroup();
			System.out.println("Error devolviendo el ButtonGroup de dificultad");
		}
		return ModoJuego;
	}
	
	private int getDificultadRadioButtons(){
		int dif=-1;
		Enumeration<AbstractButton> rbs=getDificultad().getElements();
		JRadioButton rb=(JRadioButton) rbs.nextElement();
		while(!rb.isSelected())
			rb=(JRadioButton) rbs.nextElement();
		if(rb.getText().equals("Dificil")) dif=10;
		else if(rb.getText().equals("Normal")) dif=5;
		else if(rb.getText().equals("Facil")) dif=3;
		return dif;
	}
	
	private int getModoJuegoRadioButtons(){
		int modo=-1;
		Enumeration<AbstractButton> rbs=getModoJuego().getElements();
		JRadioButton rb=(JRadioButton) rbs.nextElement();
		while(!rb.isSelected())
			rb=(JRadioButton) rbs.nextElement();
		if(rb.getText().equals("Jugador vs Jugador")) modo=0;
		else if(rb.getText().equals("Jugador vs CPU")) modo=1;
		else if(rb.getText().equals("CPU vs CPU")) modo=2;
		return modo;
	}
	private void jRadioDificilMouseClicked(MouseEvent evt) {
		System.out.println("jRadioDificil.mouseClicked, event="+evt);
		//TODO add your code for jRadioDificil.mouseClicked
		principal.setDificultad(getDificultadRadioButtons());
		System.out.println("Dificultad elegida --> " + principal.getDificultad());
	}
	
	private void jRadioNormalMouseClicked(MouseEvent evt) {
		System.out.println("jRadioNormal.mouseClicked, event="+evt);
		//TODO add your code for jRadioNormal.mouseClicked
		principal.setDificultad(getDificultadRadioButtons());
		System.out.println("Dificultad elegida --> " + principal.getDificultad());
	}
	
	private void jRadioFacilMouseClicked(MouseEvent evt) {
		System.out.println("jRadioFacil.mouseClicked, event="+evt);
		//TODO add your code for jRadioFacil.mouseClicked
		principal.setDificultad(getDificultadRadioButtons());
		System.out.println("Dificultad elegida --> " + principal.getDificultad());
	}
	
	private void setVisibilidadDificultad(boolean visible){
		Enumeration<AbstractButton> rbs=getDificultad().getElements();
		JRadioButton rb;
		for(int i=0;i<3;i++){
			rb=(JRadioButton) rbs.nextElement();
			rb.setVisible(visible);
		}
		RadioDificultadLabel.setVisible(visible);
	}
	private void jRadiocpvscpMouseClicked(MouseEvent evt) {
		System.out.println("jRadioDificil.mouseClicked, event="+evt);
		//TODO add your code for jRadioDificil.mouseClicked
		principal.setModo(getModoJuegoRadioButtons());
		setVisibilidadDificultad(true);
		System.out.println("Modo Juego elegido --> " + principal.getModo());
	}
	
	private void jRadiojvscpMouseClicked(MouseEvent evt) {
		System.out.println("jRadioNormal.mouseClicked, event="+evt);
		//TODO add your code for jRadioNormal.mouseClicked
		principal.setModo(getModoJuegoRadioButtons());
		setVisibilidadDificultad(true);
		System.out.println("Modo Juego elegido --> " + principal.getModo());
	}
	
	private void jRadiojvsjMouseClicked(MouseEvent evt) {
		System.out.println("jRadioFacil.mouseClicked, event="+evt);
		//TODO add your code for jRadioFacil.mouseClicked
		principal.setModo(getModoJuegoRadioButtons());
		setVisibilidadDificultad(false);
		System.out.println("Modo Juego elegido --> " + principal.getModo());
	}
	
	private void thisWindowClosing(WindowEvent evt) {
		System.out.println("this.windowClosing, event="+evt);
		//TODO add your code for this.windowClosing
		this.principal.setVisible(true);
	}
}


